package snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    //定义蛇的数据结构
    int length;//长度
    int[] snakex = new int[600];//蛇的坐标
    int[] snakey = new int[500];
    String fx;//初始方向向右
    boolean isstart = false;//设置游戏初始状态

    //生成食物的随机坐标
    int foodx;
    int foody;
    Random random =  new Random();


    //设置定时器
    Timer timer = new Timer(100,this);

    public GamePanel() {//构造器
        init();

        //获得焦点事件
        this.setFocusable(true);
        //获得键盘监事件
        this.addKeyListener(this);
        //游戏一开始就要启动定时器
        timer.start();

        //蛇食物坐标
        foodx = 25 +25*random.nextInt(34);
        foody = 75 +25*random.nextInt(24);

    }

    //init数据初始化
    public void init(){
        length = 3;
        //蛇的头
        snakex[0] = 100;snakey[0] = 100;//就是蛇头的坐标·
        snakex[1] = 75;snakey[1] = 100;//第一节身体的坐标
        snakex[2] = 50;snakey[2] = 100;
        fx = "R";


    }

//要画东西，就在这个类里面画
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.white);

        Data.upmage.paintIcon(this,g,1,11);
        //默认游戏界面、
        g.fillRect(18,82,850,600);

        //画蛇
        //Data.right.paintIcon(this,g,snakex[0],snakey[0]);//初始化蛇
        //设置蛇头的方向变化、
        if (fx.equals("R")){
            Data.right.paintIcon(this,g,snakex[0],snakey[0]);
        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,snakex[0],snakey[0]);
        }else if (fx.equals("U")){
            Data.header.paintIcon(this,g,snakex[0],snakey[0]);
        }else if (fx.equals("D")){
            Data.down.paintIcon(this,g,snakex[0],snakey[0]);
        }

        for (int i = 1; i <length ; i++) {
            Data.body.paintIcon(this,g,snakex[i],snakey[i]);
        }

        Data.food.paintIcon(this,g,foodx,foody);//画食物


        //游戏状态
        if(isstart == false){
            g.setColor(Color.white);
            g.setFont(new Font("文华彩云",Font.BOLD,40));//字体
            g.drawString("按下空格开始游戏",300,300);
        }
    }



    @Override
    public void keyPressed(KeyEvent e) {
        int keycode = e.getKeyCode();//获得键盘按键
        if (keycode == KeyEvent.VK_SPACE){
            isstart = !isstart;//如果按下的是空格键，则开始游戏
            repaint();
        }

        //小蛇移动，上，下，左，右
        if (keycode == KeyEvent.VK_UP){
            fx = "U";
        }else if (keycode == KeyEvent.VK_DOWN){
            fx = "D";
        } else if (keycode == KeyEvent.VK_LEFT) {
            fx = "L";
        } else if (keycode == KeyEvent.VK_RIGHT) {
            fx = "R";
        }


    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (isstart){

            //蛇吃食物
            if (snakex[0] == foodx&& snakey[0] == foody){
                length++;//蛇的长度加1
                //并且初始化food的坐标
                //蛇食物坐标
                foodx = 25 +25*random.nextInt(34);
                foody = 75 +25*random.nextInt(24);
            }

            for (int i = length-1; i >0 ; i--) {//就是让后一节移动到前面一节的位置
                snakex[i] = snakex[i-1];
                snakey[i] = snakey[i-1];
            }

            //蛇的走向 边界判断
            if (fx.equals("R")){
                snakex[0] = snakex[0]+25;
                if (snakex[0]>850) {snakex[0] = 25;}

                }else if (fx.equals("L")){
                    snakex[0] = snakex[0]-25;
                    if (snakex[0]<25){snakex[0] = 850;}

                }else if (fx.equals("U")){
                    snakey[0] = snakey[0]-25;
                    if (snakey[0]<75){snakey[0] = 650;}

                }else if (fx.equals("D")){
                    snakey[0] = snakey[0]+25;
                    if (snakey[0]>650){snakey[0] = 75;}
                }




            repaint();//移动后重画jiem
        }
            timer.start();//定时器动起来



    }










    //这两个不用
    @Override
    public void keyTyped(KeyEvent e) {}
    @Override
    public void keyReleased(KeyEvent e) {}


}
//写一个键盘监听器
